<?php

namespace App\Http\Controllers;

use App\Http\Resources\TopicResource;
use App\Models\Favorite;
use App\Http\Requests\StoreFavoriteRequest;
use App\Http\Requests\UpdateFavoriteRequest;
use App\Models\Topic;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class FavoriteController extends Controller{
    public function __construct() {
        $this->middleware(['auth:sanctum']);
    }

    public function toggle(Topic $topic){
        $user = Auth::user();
        $user->userTopics()->toggle($topic);
       return $this->success('操作成功',[
           'total'=>$topic->topicUsers()->count(),
           'is_favorite'=>$topic->topicUsers()->wherePivot('user_id',$user->id)->exists()
       ]);
    }

    //判断当前用户是否收藏了该帖子
    public function is_favorite(User $user,Topic $topic){
        return $this->success(data:[
            'is_favorite'=>$topic->topicUsers()->wherePivot('user_id',$user->id)->exists()
        ]);
    }

    public function getOneUserFavoriteTopics(){
        $user = User::find(request('u_id'));
        $topics = $user->userTopics()->latest()->paginate(5);
        return TopicResource::collection($topics);
    }
}
